package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import one.stand.entity.excel.BusinessExcelEntity;
import one.stand.mapper.BusinessCardMapperExt;
import one.stand.model.BusinessCardModel;
import one.stand.model.CompanyModel;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.service.BusinessCardService;
import one.stand.util.DateUtil;
import one.stand.util.ExportDataDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class BusinessCardServiceImpl implements BusinessCardService {

    @Autowired
    private BusinessCardMapperExt businessCardMapperExt;

    @Override
    public ResultModel<Map<String, Object>> selectCustomerList(BusinessCardRequest request) {
        log.info("查询用户列表参数：{}", JSON.toJSONString(request));
        BusinessCardModel model = new BusinessCardModel();
        model.setState(1);
        model.setIsDel(0);
        model.setUserType(request.getUserType());
        model.setName(request.getName());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<BusinessCardModel> list = businessCardMapperExt.selectCustomerList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        PageInfo<CompanyModel> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>(2);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<ExportDataDTO<BusinessExcelEntity>> selectCustomerListExport(BusinessCardRequest request) {
        log.info("导出用户列表参数：{}", JSON.toJSONString(request));
        List<BusinessCardModel> list = businessCardMapperExt.selectCustomerList(new BusinessCardModel() {{
            setState(1);
            setIsDel(0);
            setUserType(request.getUserType());
            setName(request.getName());
        }});
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<BusinessExcelEntity> excelEntities = new ArrayList<>();
        list.forEach(model -> {
            {
                BusinessExcelEntity excelEntity = new BusinessExcelEntity();
                BeanUtils.copyProperties(model, excelEntity);
                excelEntity.setCreateTime(DateUtil.formatter.format(model.getCreateTime()));
                excelEntities.add(excelEntity);
            }
        });
        ExportDataDTO exportDataDTO = new ExportDataDTO();
        exportDataDTO.setSheetName("员工列表");
        exportDataDTO.setFileName("员工列表");
        exportDataDTO.setTitle("员工列表");
        exportDataDTO.setDataList(excelEntities);
        return ResultModel.success(exportDataDTO);
    }
}
